﻿using SalesSys.DAL.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SalesSys.Comm.DAL;
using SalesSys.Comm.Utility;
using SalesSys.DAL;

namespace SalesSys.BLL
{
    public class ProviderBLL : BaseBLL<Provider, Guid>
    {
        public Dictionary<Supplier, decimal> GetNonstandardDiscount(Guid depotId)
        {
            var depot=Details(depotId);
            var dic = depot.GetNonstandardDiscountDic();
            var supBLL = BllFactory.GetBll<SupplierBLL>();
            var newDic = new Dictionary<Supplier, decimal>();
            foreach (var kv in dic)
            {
               var sup= supBLL.Details(kv.Key);
                newDic.Add(sup,kv.Value);
            }
            return newDic;
        }


        public KeyValuePair<int, WarehouseEntry[]> GetWarehouseEntrys(Guid? providerId = null, int page = 1,
            int rows = 10)
        {
            var rep = RepositoryFactory.GetRepositoryByFactoryUnit<IRepository<WarehouseEntry>>();
            var q = rep.Entities;
            if (providerId != null)
            {
                q = q.Where(p => p.ProviderId == providerId);
            }
            var count = q.Count();
            var re = q.OrderByDescending(p => p.CreateTime).Paging(page, rows).ToArray();
            return new KeyValuePair<int, WarehouseEntry[]>(count, re);
        }

        public KeyValuePair<int, Provider[]> QueryProviders(ProviderType? type = null, string name = null, int page = 1, int rows = 10)
        {
            var q=Rep.Entities;
            q = q.WhereIf(p => p.ProviderType == type, type != null);
            q = q.WhereIf(p => p.Name.Contains(name), name != null);
            var count = q.Count();
            return new KeyValuePair<int, Provider[]>(count, q.OrderByDescending(p => p.Name).Paging(page, rows).ToArray());
        }
        public KeyValuePair<int, Provider[]> QueryProviders(Guid empId, ProviderType? type = null, string name = null, int page = 1, int rows = 10)
        {
            var q = Rep.Entities;
            q = q.WhereIf(p => p.ProviderType == type, type != null);
            q = q.WhereIf(p => p.Name.Contains(name), name != null);
            q = q.Where(p => p.MainFieldMan.Id == empId);
            var count = q.Count();
            return new KeyValuePair<int, Provider[]>(count, q.OrderByDescending(p => p.Name).Paging(page, rows).ToArray());
        }

        /// <summary>
        /// 获取仓库历史,只填充商品对象
        /// </summary>
        /// <param name="depotId"></param>
        /// <param name="dt"></param>
        /// <returns></returns>
        public IEnumerable<DepotHistroy> GetDepotHistroy(Guid depotId, DateTime dt)
        {
            var hisRep = RepositoryFactory.GetRepositoryByFactoryUnit<IDepotHistroyRep>();
            return hisRep.GetDepotHistroy(depotId, dt);
        }

          
    }
}
